<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\Limit;
use app\admin\model\Product;
use think\cache\driver\Redis;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Good extends BaseController
{
    //添加商品
    public function goodAdd()
    {
        return View::fetch();
    }
    //修改商品
    public function goodEdit()
    {
        return View::fetch();
    }
    //商品列表
    public function goodList()
    {
        $good = Product::alias('p')
            ->field('p.*,c.name as cname')
            ->join('category c','p.cid=c.id')
            ->paginate(
                [
                    'list_rows'=>5,
                ]
            );
        $page = $good->render();
        View::assign(['good'=>$good,'page'=>$page]);
        $limit = Limit::column('pid');
        View::assign('limit',$limit);
        return View::fetch();
    }
    //商品秒杀添加
    public function goodLimit()
    {
        $redis = new Redis();
        $redis->connect("127.0.0.1",6379);
        $redis->auth('123456');
        if (Request::isPost()){
            $post = Request::post();
            $limit = Limit::where('pid','=',$post['good_id'])->find();
            if ($limit==null){
                $data = [
                    'pid'=>$post['good_id'],
                    'name'=>$post['name'],
                    'price'=>$post['price'],
                    'num'=>$post['num'],
                    'photo'=>$post['img'],
                    'addtime'=>strtotime($post['addtime']),
                    'endtime'=>strtotime($post['endtime'])
                ];
                try{
                    $add = Limit::insertGetId($data);
                }catch (\Exception $e){
                    $add = false;
                    echo $e->getMessage();
                }
                if ($add){
                    $ms = Limit::where('id','=',$add)->find();
                    for ($i=1;$i<=$ms['num'];$i++){
                        $redis->rpush($ms['id'],$i);
                    }
                    $this->success('添加成功',url('good/goodList'));
                }else{
                    $this->error('添加失败');
                }
            }
        }else{
            $get = Request::get();
            $good = Product::where('id','=',$get['id'])->find();
            View::assign('good',$good);
        }
        return View::fetch();
    }
    //秒杀商品表
    public function goodLimitList()
    {
        $good = Limit::paginate(
            [
                'list_rows'=>5,
            ]
        );
        $page = $good->render();
        View::assign(['good'=>$good,'page'=>$page]);
        return View::fetch();
    }
    //秒杀商品删除
    public function limitDel()
    {
        $get = Request::get();
        Limit::destroy($get['id']);
        $this->success('删除成功',url('order/orderList'));
    }
    //秒杀商品修改
    public function goodLimitEdit()
    {
        $redis = new Redis();
        $redis->connect("127.0.0.1",6379);
        $redis->auth('123456');
        if (Request::isPost()){
            $post = Request::post();
            try{
                $edit = Limit::update(['id'=>$post['id'],'price'=>$post['price'],'num'=>$post['num'],'addtime'=>strtotime($post['addtime']),'endtime'=>strtotime($post['endtime'])]);
            }catch (\Exception $e){
                $edit = false;
            }
            if ($edit){
                $redis->rpush($post['id'],$post['num']);
                $this->success('修改成功',url('good/goodLimitList'));
            }else{
                $this->error('修改成功');
            }
        }else{
            $get = Request::get();
            $limit = Limit::where('id','=',$get['id'])->find();
            $limit['addtime']=date('Y-m-d',$limit['addtime'])."T".date('H:i:s',$limit['addtime']);
            $limit['endtime']=date('Y-m-d',$limit['endtime'])."T".date('H:i:s',$limit['endtime']);
            View::assign('limit',$limit);
        }

        return View::fetch();
    }







}